package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CommunityPost;
import com.ruoyi.system.domain.CommunityComment;
import com.ruoyi.system.domain.CommunityLike;
import com.ruoyi.system.mapper.CommunityPostMapper;
import com.ruoyi.system.mapper.CommunityCommentMapper;
import com.ruoyi.system.mapper.CommunityLikeMapper;
import com.ruoyi.system.service.ICommunityService;

/**
 * 社区服务实现类
 * 
 * @author ruoyi
 */
@Service
public class CommunityServiceImpl implements ICommunityService 
{
    @Autowired
    private CommunityPostMapper postMapper;

    @Autowired
    private CommunityCommentMapper commentMapper;

    @Autowired
    private CommunityLikeMapper likeMapper;

    @Override
    public List<CommunityPost> selectPostList(CommunityPost post) {
        return postMapper.selectPostList(post);
    }

    @Override
    public CommunityPost selectPostById(Long postId) {
        return postMapper.selectPostById(postId);
    }

    @Override
    @Transactional
    public int insertPost(CommunityPost post) {
        post.setCreateTime(DateUtils.getNowDate());
        post.setLikes(0);
        post.setComments(0);
        post.setStatus("0");
        return postMapper.insertPost(post);
    }

    @Override
    public int updatePost(CommunityPost post) {
        return postMapper.updatePost(post);
    }

    @Override
    public int deletePostById(Long postId) {
        return postMapper.deletePostById(postId);
    }

    @Override
    public int deletePostByIds(Long[] postIds) {
        return postMapper.deletePostByIds(postIds);
    }

    @Override
    public List<CommunityComment> selectCommentList(Long postId, Long userId) {
        CommunityComment comment = new CommunityComment();
        comment.setPostId(postId);
        comment.setUserId(userId);
        return commentMapper.selectCommentList(comment);
    }

    @Override
    public CommunityComment selectCommentById(Long commentId) {
        return commentMapper.selectCommentById(commentId);
    }

    @Override
    @Transactional
    public int insertComment(CommunityComment comment) {
        comment.setCreateTime(DateUtils.getNowDate());
        comment.setLikes(0);
        comment.setStatus("0");
        int rows = commentMapper.insertComment(comment);
        if (rows > 0) {
            postMapper.updatePostComments(comment.getPostId(), 1);
        }
        return rows;
    }

    @Override
    public int updateComment(CommunityComment comment) {
        return commentMapper.updateComment(comment);
    }

    @Override
    public int deleteCommentById(Long commentId) {
        return commentMapper.deleteCommentById(commentId);
    }

    @Override
    public int deleteCommentByIds(Long[] commentIds) {
        return commentMapper.deleteCommentByIds(commentIds);
    }

    @Override
    public List<CommunityPost> selectLikedPostList(Long userId, CommunityPost post) {
        // 在这里调用 Mapper 的方法来获取点赞过的帖子列表
        return postMapper.selectLikedPostListByUserId(userId, post);
    }

    @Override
    public CommunityLike selectLikeByUserAndTarget(Long userId, Long targetId, String type) {
        return likeMapper.selectLikeByUserAndTarget(userId, targetId, type);
    }

    @Override
    @Transactional
    public int insertLike(CommunityLike like) {
        like.setCreateTime(DateUtils.getNowDate());
        return likeMapper.insertLike(like);
    }

    @Override
    public int deleteLikeById(Long likeId) {
        return likeMapper.deleteLikeById(likeId);
    }

    @Override
    public int updatePostLikes(Long postId, Integer count) {
        return postMapper.updatePostLikes(postId, count);
    }

    @Override
    public int updatePostComments(Long postId, Integer count) {
        return postMapper.updatePostComments(postId, count);
    }

    @Override
    public int updateCommentLikes(Long commentId, Integer count) {
        return commentMapper.updateCommentLikes(commentId, count);
    }

    @Override
    @Transactional
    public int like(String type, Long id) {
        Long userId = SecurityUtils.getUserId();
        CommunityLike like = likeMapper.selectLikeByUserAndTarget(userId, id, type);
        
        if (like != null) {
            // 取消点赞
            likeMapper.deleteLikeById(like.getLikeId());
            if ("1".equals(type)) {
                postMapper.updatePostLikes(id, -1);
            } else {
                commentMapper.updateCommentLikes(id, -1);
            }
            return 0;
        } else {
            // 添加点赞
            like = new CommunityLike();
            like.setUserId(userId);
            like.setTargetId(id);
            like.setType(type);
            like.setCreateTime(DateUtils.getNowDate());
            likeMapper.insertLike(like);
            if ("1".equals(type)) {
                postMapper.updatePostLikes(id, 1);
            } else {
                commentMapper.updateCommentLikes(id, 1);
            }
            return 1;
        }
    }

    @Override
    public List<CommunityPost> selectCommentedPostList(Long userId, CommunityPost post) {
        return postMapper.selectCommentedPostListByUserId(userId, post);
    }

    @Override
    public List<CommunityLike> selectLikeList(CommunityLike like) {
        return likeMapper.selectLikeList(like);
    }

    @Override
    public List<CommunityComment> selectAllCommentList(CommunityComment comment) {
        return commentMapper.selectCommentList(comment);
    }

    private List<CommunityComment> buildCommentTree(List<CommunityComment> comments) {
        List<CommunityComment> topLevelComments = new java.util.ArrayList<>();
        java.util.Map<Long, CommunityComment> commentMap = new java.util.HashMap<>();

        // 将所有评论放入Map，方便查找
        for (CommunityComment comment : comments) {
            commentMap.put(comment.getCommentId(), comment);
            // 初始化children列表，避免空指针
            comment.setReplies(new java.util.ArrayList<>());
        }

        // 构建父子关系
        for (CommunityComment comment : comments) {
            // 如果有父评论且父评论存在于Map中，则将其添加到父评论的replies列表中
            if (comment.getParentId() != null && comment.getParentId() != 0 && commentMap.containsKey(comment.getParentId())) {
                commentMap.get(comment.getParentId()).getReplies().add(comment);
            } else {
                // 否则，认为是顶层评论
                topLevelComments.add(comment);
            }
        }
        return topLevelComments;
    }
}